package com.h3c.ptability.utils;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.eos.common.util.IdWorker;
import com.h3c.ptability.cache.UserCache;
import com.h3c.ptability.config.mq.EsignRabbitMqConfig;
import com.h3c.ptability.config.mq.LogRabbitConfig;
import com.h3c.ptability.dto.SdAuthChgLogDTO;
import com.h3c.ptability.enums.AutoOpTypeEnum;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author chenyifan
 * @date 2022/6/19 15:57
 * mq工具类
 */
@Component
public class MqUtils {

    private static RabbitTemplate rabbitTemplate;

    private static LogRabbitConfig rabbitConfig;

    /**
     * 新员工侧签署中状态枚举
     */
    public static final Integer WAIT_SIGN_CODE = 6;

    /**
     * 新员工侧签署中状态枚举
     */
    public static final Integer NEM_SIGNING_CODE = 7;
    /**
     * 新员工侧签署完成状态枚举
     */
    public static final Integer NEM_SIGN_SUCCESS_CODE = 8;
    /**
     * 新员工侧拒绝签署状态枚举
     */
    public static final Integer NEM_SIGN_REJECT_CODE = 12;
    /**
     * 新员工侧签署异常状态枚举
     */
    public static final Integer NEM_SIGN_FAIL_CODE = 55;
    //------------------------------------------------------------

    /**
     * 人才协议待签署
     */
    public static final Integer TALENT_SIGN_ING = 0;
    /**
     * 人才协议已签署
     */
    public static final Integer TALENT_ALREADY_SIGN = 1;
    /**
     * 人才协议拒签
     */
    public static final Integer TALENT_REJECT_SIGN = 2;
    /**
     * 人才协议已作废
     */
    public static final Integer TALENT_CANCEL_SIGN = 3;

    @Autowired
    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        MqUtils.rabbitTemplate = rabbitTemplate;
    }

    @Autowired
    public void setRabbitConfig(LogRabbitConfig rabbitConfig) {
        MqUtils.rabbitConfig = rabbitConfig;
    }

    private static Message getMessage(String messageBody) {
        return MessageBuilder.withBody(messageBody.getBytes())
                .setContentType(MessageProperties.CONTENT_TYPE_JSON)
                .setContentEncoding("utf-8")
                .setMessageId(UUID.randomUUID() + "")
                .build();
    }

    /**
     * 推送签署状态到新员工队列
     * @param workerId
     * @param workerStatus
     */
    public static void pushSignStatusToNem(String workerId, Integer workerStatus) {
        HashMap<String, Object> param = new HashMap<>(2);
        param.put("workerId", workerId);
        param.put("signStatus", workerStatus);
        rabbitTemplate.convertAndSend(EsignRabbitMqConfig.NEM_EXCHANGE_NAME, "nem.es.sign", getMessage(JSONUtil.toJsonStr(param)));
    }

    /**
     * 推送人才津贴签署信息到新员工人才签署队列
     * @param workerId
     * @param workerStatus
     * @param rejectReason
     */
    public static void pushSignStatusToNemTalent(String workerId, Integer workerStatus, String rejectReason) {
        HashMap<String, Object> param = new HashMap<>(3);
        param.put("workerId", workerId);
        param.put("talentBonusSignState", workerStatus);
        param.put("rejectReason", rejectReason);
        rabbitTemplate.convertAndSend(EsignRabbitMqConfig.NEM_EXCHANGE_NAME, "nem.es.sign.talent", getMessage(JSONUtil.toJsonStr(param)));
    }

    /**
     * 推送邮件任务到新员工系统
     * @param workerId
     * @param signUrl
     * @param templateCode
     */
    public static void pushEmailJobToNem(String workerId, String signUrl, String templateCode) {
        HashMap<String, Object> param = new HashMap<>(3);
        param.put("workerId", workerId);
        param.put("signUrl", signUrl);
        param.put("templateCode", templateCode);
        rabbitTemplate.convertAndSend(EsignRabbitMqConfig.NEM_EXCHANGE_NAME, "nem.email", getMessage(JSONUtil.toJsonStr(param)));
    }

    /**
     * 推送签署信息到hr-sign系统
     * @param workerId
     * @param recordId
     * @param sceneType
     */
    public static void pushSignInfoToHrSign(String workerId, String recordId, Integer sceneType) {
        HashMap<String, Object> param = new HashMap<>(3);
        param.put("workerId", workerId);
        param.put("recordId", recordId);
        param.put("sceneType", sceneType);
        rabbitTemplate.convertAndSend(EsignRabbitMqConfig.HR_SIGN_EXCHANGE_NAME, "hr.es.sign", getMessage(JSONUtil.toJsonStr(param)));
    }

    /**
     * 权限操作日志
     *
     * @param opTableName 操作表
     * @param opType      操作类型
     * @param dataJson    接口数据
     * @param remark      备注
     */
    public static void recordAuthChangeLog(String opTableName, AutoOpTypeEnum opType, String dataJson, String remark) {
        SdAuthChgLogDTO dto = new SdAuthChgLogDTO();
        dto.setLogId(IdWorker.nextId() + "");
        dto.setAppId("nem-manager");
        dto.setOperUserId(UserCache.getLoginUserId());
        dto.setOperUserName(UserCache.getLoginUserDTO().getUserName());
        dto.setOperTime(new Date());
        dto.setOperTableName(opTableName);
        dto.setOperType(opType.getCode());
        dto.setDataJson(dataJson);
        dto.setRemark(remark);
        Message message = getMessage(JSONObject.toJSONString(dto));
        rabbitTemplate.convertAndSend(rabbitConfig.getExchange(), rabbitConfig.getAuthChgLogQueue(), message);
    }

}
